package com.zhu.edu.controller;


import com.alibaba.fastjson.JSON;
import com.zhu.edu.entity.User;
import com.zhu.edu.service.UserService;
import com.zhu.edu.token.JwtToken;
import com.zhu.edu.util.JwtUtil;
import com.zhu.edu.util.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjc
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequiresPermissions("course:choose")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ApiOperation("测试权限")
    public String test() {
        return "你好啊";
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    @ApiOperation("测试前后端交互")
    public String test2() {
        return "我很好";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation("登录")
    public String login(@RequestBody User user) {
        if (user.getPassword() == null || user.getUsername() == null) {
            return JSON.toJSONString(new Result().setCode(401).setMessage("用户名密码不正确"));
        }
        // 获取当前登录用户的信息
        Subject subject = SecurityUtils.getSubject();
        String jwt = JwtUtil.createJWT(user.getUsername(), "back", "user", 1000 * 60 * 30);
       // 创建令牌
        JwtToken jwtToken = new JwtToken(jwt, user.getPassword());
        try {
            subject.login(jwtToken);
        } catch (UnknownAccountException e) {
            return JSON.toJSONString(new Result().setCode(401).setMessage("账号不存在!!!"));
        } catch (IncorrectCredentialsException e) {
            return JSON.toJSONString(new Result().setCode(401).setMessage("密码错误!!!"));
        }
        User backUser = userService.getUserByUsername(user.getUsername());
        backUser.setPassword(null);
        backUser.setSalt(null);
        Map<String, Object> map = new HashMap<>();
        map.put("user", backUser);
        map.put("token", jwt);
        return JSON.toJSONString(new Result().setCode(200).setMessage("登陆成功").setData(map));
    }

    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
    @ApiOperation("侧边栏")
    public String getMenuList(String username) {
        if (StringUtils.isEmpty(username)) {
            return JSON.toJSONString(new Result().setCode(401).setMessage("未登录"));
        }
        List<Map<String, Object>> list = userService.getMenuList(username);
        return JSON.toJSONString(new Result().setCode(200).setData(list));
    }
    @ApiOperation("获取学院所有老师")
    @RequestMapping(value = "/getTeachers", method = RequestMethod.GET)
    public String getTeachersBySchoolId(Integer schoolId) {
        List<User> list = userService.getAllTeachersBySchoolId(schoolId);
        return JSON.toJSONString(new Result().setCode(200).setData(list));
    }

}
